package org.brisling.sysman.queryService;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.brisling.base.domain.ListDomain;
import org.brisling.base.service.AbstractBaseQueryService;
import org.brisling.common.ConstSysParam;
import org.brisling.common.exception.DangerousOperatorException;
import org.brisling.sysman.domain.RolesPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/rolePermission/query")
public class QueryRolePermissionService extends AbstractBaseQueryService<RolesPermission>{
	
	final String PERMISSION_BY_ROLE_SQL = "SELECT * FROM %1$s WHERE %2$s ORDER BY SORTEDID;";
	
		
	final String ADMIN_MENU_SQL = "SELECT DISTINCT p.*  FROM %1$s p WHERE p.isused='Y' AND p.status='A' ORDER BY SORTEDID;";
	
	
	//1,Permissions;2,RolesPermission;3,UserRoles;4,uid
	final String NORMAL_MENU_SQL = "SELECT DISTINCT p.* FROM %1$s p " +
			"LEFT JOIN %2$s rp ON p.id = rp.permissionid "+
			"LEFT JOIN %3$s ur ON rp.roleid = ur.roleid "+			
			"WHERE ur.userid=%4$d and p.isused='Y' AND p.status='A';";
	
	public QueryRolePermissionService(){
		this.moduleName = ConstSysParam.MODULE_ROLE_PERMISSION;
		this.subSystem = ConstSysParam.SUBSYSTEM_SYSMAN;
		this.tableName = ConstSysParam.VIEW_PREFIX +this.getTableName(RolesPermission.class);
	}
	
	@PersistenceContext(unitName=ConstSysParam.SUBSYSTEM_SYSMAN)
	@Autowired
	protected void setEntityManager(EntityManager entityManager){
		this.entityManager = entityManager;		
	}
	
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return this.getBizMainPage();
	}
	
	/* (non-Javadoc)
	 * @see org.brisling.service.sysman.DepartmentServiceInf#getEditPage()
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return this.getBizEditPage();
	}
	
	
	
	
	@RequestMapping("/getListDomain.action")
	public ListDomain<RolesPermission> getListDomain(@RequestBody(required=false) String body){		
		
		return super.getListDomainByConditionAndOrder(body, typeMap, orderMap, true);
		
	}
	/**
	 * 根据角色编码查询权限清单
	 * @param roleid 	
	 * @return	
	 * @throws DangerousOperatorException 
	 */
	
	@RequestMapping("/getPermissionByRole.action")
	public ListDomain<RolesPermission> getPermissionByRole(@RequestParam(value="roleid",required=false) Long roleid		
			) throws DangerousOperatorException{
		
		if(roleid!=null){
			StringBuilder whereCorse = new StringBuilder();
			whereCorse.append("roleid=");
			whereCorse.append(roleid);
			String qry=String.format(PERMISSION_BY_ROLE_SQL, this.tableName,whereCorse);
			return super.getSqlListDomain(qry);
		}
		return null;			
	}
		
	@RequestMapping("/getOne.action")
	public RolesPermission getOne(@RequestParam(value="id",required=false) Long id){
		return super.getOne(id);
	}
	
	/**
	 * 
	 * @return 用户具备访问权限的业务模块清单
	 * @throws DangerousOperatorException 
	 * @throws Exception 
	 */
	
	
	

}
